package com.mgm.custom.mobpals;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mgm.base.ConverAdRoot;
import com.mgm.pojo.Advertisement;

public class MobpalsConverToAdvertisement {
	
	protected static Map<String, String> plmap = new HashMap<>();
	static {
		plmap.put("1", "ios");
		plmap.put("2", "android");
	}
	
	public static JSONArray checkOffer(JSONArray jsonArray) {
		/**
		 * offerid,offername,trackingurl,payouttype,geo,packagename,os／platform
		 * payout
		 */
		Iterator<Object> it = jsonArray.iterator();
		while(it.hasNext()) {
//			JSONObject j_obj = jsonArray.getJSONObject(i);
			JSONObject j_obj = (JSONObject) it.next();
			String offerid = j_obj.getString("offerId");
			String offername = j_obj.getString("name");
			String trackingurl = j_obj.getString("clickUrl");
			String payouttype = j_obj.getString("conversionFlow");
			String geo = j_obj.getString("country2B"); //上游建议取这个
			String packagename = j_obj.getString("pack");
			String os = j_obj.getString("os");
			
			int v = 0;
			if(!StringUtils.isEmpty(j_obj.getString("bid")) && j_obj.getBigDecimal("bid").compareTo(BigDecimal.ZERO)==1 ) { //单价
				v = 1;
			}
			
			if(StringUtils.isEmpty(offerid) || StringUtils.isEmpty(offername) 
					|| StringUtils.isEmpty(trackingurl) || StringUtils.isEmpty(payouttype)
					|| StringUtils.isEmpty(geo) || StringUtils.isEmpty(packagename) 
					|| StringUtils.isEmpty(os) || v==0) {
				it.remove();
			}
			
			/** 过滤一些数据 */
			//上游offer的status=1才是线上的
			//目前只做cpi的offer
			if(j_obj.getInteger("status") != 1 || !"cpi".equalsIgnoreCase(j_obj.getString("conversionFlow"))) it.remove();
			
		}
		return jsonArray;
	}
	
	/**
	 * 
	 * @Title: toAdvertisement   
	 * @Description: 转化成我们的offer   
	 * @param: @param aderId
	 * @param: @param jsonArray
	 * @param: @return      
	 * @return: ConverAdRoot      
	 * @throws
	 */
	public static ConverAdRoot toAdvertisement(Integer aderId, JSONArray jsonArray) {
		if(jsonArray == null) {
			return null;
		}
		ConverAdRoot root = new ConverAdRoot();
		root.setAdId(aderId);
		Date curTime = new Date();
    	DateTime jodaTime = new DateTime(curTime);
    	DateTime n = jodaTime.plusHours(2);
    	
		int size = jsonArray.size(); //数组大小
		Advertisement adment = null;
		List<Advertisement> adlist = new ArrayList<>(size);
		Map<String, Advertisement> admap = new HashMap<>(size);
		List<String> adIds = new ArrayList<>(size);
		for (int i = 0; i < size; i++) {
			JSONObject j_obj = jsonArray.getJSONObject(i);
			
			//----广告===========================
			adment = new Advertisement();
//			String offerid = j_obj.getString("id")+RandomCodeUtils.randomNumeric(8); //系统生成offerid
			adment.setAdv_id(aderId); 
//			adment.setOfferId(offerid); 
			adment.setOfferName(j_obj.getString("name"));
			adment.setEnabled(true);
			
			adment.setPay_type(j_obj.getString("conversionFlow")); //默认cpi
			adment.setPay_price(j_obj.getBigDecimal("bid")); //上游bid是单价
			adment.setCurrency("USD"); //默认USD
			adment.setRequires(null); //不做强制
			
			adment.setOs(j_obj.getString("os"));
			adment.setCountries(j_obj.getString("country2B"));
			adment.setClick_url(j_obj.getString("clickUrl"));
			adment.setCarrier(j_obj.getString("carrier"));
			adment.setOs_version(j_obj.getString("minVersion"));
			adment.setExcitation(null);
    		adment.setExpire_date(n.toDate());
    		adment.setTrackingsolution(null); //检测类型Monitoring (adjust/appsflyer/mat/reyun/talkingdata)
    		adment.setKpi(null);
    		
    		adment.setCap_type(null);
    		adment.setConvernsion_cap(null);
    		adment.setDaily_conversion_cap(null);
    		adment.setMonth_conversion_cap(null);
    		adment.setPayout_cap(null);
    		adment.setDaily_payout_cap(null);
    		adment.setMonth_payout_cap(null);
    		
    		adment.setAppname(j_obj.getString("name"));
    		adment.setPkgname(j_obj.getString("pack"));
    		adment.setPreview_url(j_obj.getString("preview_url"));
    		
    		adment.setPremium(null);
    		adment.setPremium_price(null);
    		adment.setIcon(j_obj.getString("icon"));
    		adment.setStart_at(curTime);
    		adment.setEnd_at(null);
    		adment.setUpdate_time(curTime);
    		adment.setCounter(0); //默认0
    		adment.setP1(j_obj.getString("offerId")); //上游offerid
    		
    		//----素材======================
    		
    		adlist.add(adment);
    		admap.put(adment.getP1(), adment);
    		adIds.add(j_obj.getString("offerId"));
		}
		root.setAdlist(adlist);
		root.setAdmap(admap);
		root.setAdids(adIds);
		return root;
	}

}
